<template>
	<view>
		<!-- 地址 -->
		<navigator url="/pages/address/address?source=1" class="address-section">
			<view class="order-content">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen">
					<block v-if="addressData.name">
						<view class="top">
							<text class="name">{{addressData.name}}</text>
							<text class="mobile">{{addressData.phone}}</text>
						</view>
						<text class="address">{{addressData.province}}{{addressData.city!='市辖区'?addressData.city:''}}{{addressData.area}}{{addressData.remark}}</text>
					</block>
					<block v-if="!addressData.name">
						<text class="name">选择收货地址</text>
					</block>
				</view>
				<text class="yticon icon-you"></text>
			</view>

			<image class="a-bg" src=""></image>
		</navigator>

		<view class="goods-section">
			<!-- 商品列表 -->
			<view v-for="(item,index) in goodsData" :key="index" class="g-item">
				<image :src="item.image"></image>
				<view class="right">
					<text class="title clamp">{{item.productName}}</text>
					<view class="price-box">
						<text class="price">￥{{isAgent == 0 ? item.price : item.agentPrice}}</text>
						<text class="number">x {{item.productNum}}</text>
					</view>
					<view v-if="item.productActivity && item.productActivity.status==0" style="color: #FA436A;font-size: 24rpx;" class="price-box">{{item.productActivity.content}}</view>
					<text class="spec">小计：<text>￥{{item.total}}</text></text>
				</view>
			</view>
		</view>

		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip">￥{{totalPrice}}</text>
			</view>
			<picker mode="selector" :range="psList" range-key="name" @change="psListChange">
				<view class="yt-list-cell b-b">
					<text class="cell-tit clamp">配送方式</text>
					<text class="cell-tip">{{psList[orderType].name}}</text>
					<text class="yticon icon-you"></text>
				</view>
			</picker>
			<!-- <view class="yt-list-cell b-b">
				<text class="cell-tit clamp">优惠金额</text>
				<text class="cell-tip red">-￥35</text>
			</view> -->
			<!-- <view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip">免运费</text>
			</view> -->
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="desc" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{totalPrice}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>

	</view>
</template>

<script>
	let postIng = false;
	export default {
		data() {
			return {
				orderType: 0,
				psList: [{
						name: '快递配送',
						type: 0

					},
					{
						name: '同城配送',
						type: 1
					}
				],
				desc: '', //备注
				payType: 1, //1微信 2支付宝
				addressData: {},
				goodsData: [],
				isAgent: 0,
				totalPrice: 0,
			}
		},

		onLoad(option) {
			console.log('结算页面----onLoad', option);
			if (uni.getStorageSync('userInfo')) {
				this.isAgent = uni.getStorageSync('userInfo').isAgent;
				//商品数据
				let data = JSON.parse(option.data);
				data.goodsData.forEach(o => {
					let price = this.isAgent == 0 ? o.price : o.agentPrice;
					o.total = Number(price) * Number(o.productNum);
					if (o.productActivity && o.productActivity.status == 0) {
						if (o.productActivity.type == 0 && Number(o.total) >= Number(o.productActivity.price)) {
							//0单次满减活动
							o.total = Number(o.total) - Number(o.productActivity.reductionPrice);
						}
					}
					this.totalPrice += o.total;
				})
				this.goodsData = data.goodsData;

				//获取收货
				this.getAddressInfo();
			}
		},
		methods: {

			//-------------------------------点击---------------------------------------------
			//提交订单
			submit() {
				if (!postIng) {
					postIng = true;
					let orderParamList = [];
					this.goodsData.forEach(o => {
						let map = {
							productId: o.productId,
							num: o.productNum
						}
						orderParamList.push(map);
					})
					//创建订单
					var data = {
						userCode: uni.getStorageSync('userInfo').userCode,
						consignee: this.addressData.name,
						province: this.addressData.province,
						city: this.addressData.city,
						area: this.addressData.area,
						address: this.addressData.remark,
						remark: this.desc,
						phone: this.addressData.phone,
						orderType: this.orderType,
						orderParamList: orderParamList
					}
					this.createOrder(data)
				}
			},

			//-------------------------------函数---------------------------------------------
			//获取收货地址
			getAddressInfo: function() {
				let than = this;
				than.$api.myPost({
					url: 'addressInfoList',
					data: {
						userCode: uni.getStorageSync('userInfo').userCode,
						pageNo: 1,
						pageSize: 1000
					},
					success(res) {
						console.log('获取收货地址---', res)
						if (res.status == 'success' && res.userAddressList) {
							if (res.userAddressList.length > 0) {
								res.userAddressList.forEach(o => {
									if (o.isDefault == 1) {
										than.addressData = o;
									}
								})
								if (Object.keys(than.addressData).length == 0) {
									than.addressData = res.userAddressList[0];
								}
							}
						}
					}
				})
			},

			//创建订单
			createOrder: function(data) {
				let than = this;
				than.$api.myPost({
					url: 'createOrder',
					data: data,
					success(res) {
						postIng = false;
						if (res.status == 'success' && res.orderCode) {
							let data = {
								pageCode: 'createOrder',
								orderCode: res.orderCode,
								price: than.totalPrice
							}
							uni.redirectTo({
								url: '/pages/money/pay?data=' + encodeURIComponent(JSON.stringify(data))
							})
						} else {
							than.$api.msg(res.message);
						}
					}
				})
			},
			/*-------------------------------监听--------------------------------------------*/
			//配送方式监听
			psListChange: function(e) {
				this.orderType = e.detail.value;
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}

	.goods-section {
		margin: 0 2.5%;
		width: 95%;
		box-shadow: 0 20upx 40upx -16upx rgba(0, 0, 0, .1);
		box-shadow: 1px 2px 5px rgba(0, 0, 0, .1);
		border-radius: 16rpx;
		margin-top: 20upx;
		background: #fff;
		padding: 20rpx 30rpx;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 10upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 6upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
				font-weight: bold;
			}

			.spec {
				font-size: 28upx;

				// color: $font-color-light;
				text {
					font-size: 30upx;
					color: #FA436A;
					font-weight: bold;
				}
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 26upx;
				color: $font-color-base;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin: 0 2.5%;
		margin-top: 20upx;
		background: #fff;
		width: 95%;
		box-shadow: 0 20upx 40upx -16upx rgba(0, 0, 0, .1);
		box-shadow: 1px 2px 5px rgba(0, 0, 0, .1);
		border-radius: 16rpx;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}

		.icon-alipay {
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}

		.tit {
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: column;
		margin: 20upx 24upx;
		background: #fff;

		.con {
			display: flex;
			align-items: center;
			position: relative;
			height: 120upx;
			padding: 0 30upx;

			&:after {
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}

		.left {
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100upx;
		}

		.title {
			font-size: 32upx;
			color: $font-color-dark;
			margin-bottom: 10upx;
		}

		.time {
			font-size: 24upx;
			color: $font-color-light;
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26upx;
			color: $font-color-base;
			height: 100upx;
		}

		.price {
			font-size: 44upx;
			color: $base-color;

			&:before {
				content: '￥';
				font-size: 34upx;
			}
		}

		.tips {
			font-size: 24upx;
			color: $font-color-light;
			line-height: 60upx;
			padding-left: 30upx;
		}

		.circle {
			position: absolute;
			left: -6upx;
			bottom: -10upx;
			z-index: 10;
			width: 20upx;
			height: 20upx;
			background: #f3f3f3;
			border-radius: 100px;

			&.r {
				left: auto;
				right: -6upx;
			}
		}
	}
</style>
